package ${packageName};

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

<#list importList! as each>
import ${each};
</#list>

@Controller
@RequestMapping("/${domainVariable}")
public class ${prefix!}${domain}${surffix!}<#if extendsStr??> extends ${extendsStr!}</#if> <#list implementsList as each><#if each_index == 0> implements</#if> ${each}<#if each_has_next>, </#if></#list>{
	@RequestMapping("/form")
	public ModelAndView form(@RequestParam(defaultValue="0") int id) {
		${domain} ${domainVariable} = ${domainVariable}DAO.select(id);
		ModelAndView mav = new ModelAndView("${domainVariable}/form");
		mav.addObject("${domainVariable}", ${domainVariable});
		return mav;
	}

	@RequestMapping("/insert")
	public ModelAndView insert(${domain} ${domainVariable}, HttpServletRequest request) throws Exception {
		int id = ${domainVariable}DAO.insert(${domainVariable});
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/${domainVariable}/view")));
		mav.addObject("id", id);
		return mav;
	}

	@RequestMapping("/update")
	public ModelAndView update(${domain} ${domainVariable}) throws Exception {
		${domainVariable}DAO.update(${domainVariable});
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/${domainVariable}/view")));
		mav.addObject("id", ${domainVariable}.getId());
		return mav;
	}
	
	@RequestMapping("/delete")
	public ModelAndView delete(int id) {
		${domainVariable}DAO.delete(id);
		ModelAndView mav = new ModelAndView(new RedirectView(getRedirectUrl("/${domainVariable}/list")));
		return mav;
	}
	
	@RequestMapping("/view")
	public ModelAndView view(int id) {
		${domain} ${domainVariable} = ${domainVariable}DAO.select(id);
		if (${domainVariable} == null) {
			throw new SaleException("message");
		}
		ModelAndView mav = new ModelAndView("${domainVariable}/view");
		mav.addObject("${domainVariable}", ${domainVariable});
		return mav;
	}	
	
	@RequestMapping("/list")
	public ModelAndView list(${domain}Search search, PageNavigator pageNavigator) {
		search.setOffset(pageNavigator.getOffset());
		search.setLimit(pageNavigator.getListSize());
		List<${domain}> ${domainVariable}List = ${domainVariable}DAO.search(search);
		
		ModelAndView mav = new ModelAndView("${domainVariable}/list");
		mav.addObject("search", search);
		mav.addObject("pageNavigator", pageNavigator);
		mav.addObject("${domainVariable}List", ${domainVariable}List);
		return mav;
	}
}
